package com.hotelManager.www.controller;

import com.hotelManager.www.po.*;
import com.hotelManager.www.service.*;
import com.hotelManager.www.common.Result;
import com.hotelManager.www.common.ResultCode;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
 * 管理员控制器
 */
public class AdminController {

    private final UserService userService;
    private final ShopService shopService;
    private final SignInService signInService;

    public AdminController() {
        this.userService = new UserService();
        this.shopService = new ShopService();
        this.signInService = new SignInService();
        new OrderService();
    }

   //查看商家店铺信息
    public Result<List<Shop>> getAllMerchants() {
        try {
            List<User> merchants = userService.getUsersByRole("Merchant");
            List<Shop> merchantShops = new ArrayList<>();

            for (User merchant : merchants) {
                Shop shop = shopService.getShopByUserId(merchant.getId());
                if (shop != null) {
                    merchantShops.add(shop);
                }
            }

            return Result.success(merchantShops);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    
    //查看所有用户信息
    public Result<List<User>> getAllUsersByRoles(List<String> roles) {
        try {
            List<User> users = userService.getUsersByRoles(roles);
            return Result.success(users);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }




//获取待审核的商家
    public Result<List<Shop>> getPendingShops() {
        try {
            List<Shop> shops = shopService.getPendingShops();
            return Result.success(shops);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
    //通过店铺审核
    public Result<Void> approveShop(Integer shopId) {
        try {
            boolean success = shopService.updateShopAuditStatus(shopId, "approved");
            if (success) {
                return Result.success(null, "店铺审核通过");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "店铺审核失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//拒绝商家店铺审核
    public Result<Void> rejectShop(Integer shopId) {
        try {
            boolean success = shopService.updateShopAuditStatus(shopId, "rejected");
            if (success) {
                return Result.success(null, "店铺审核拒绝");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "店铺审核失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//封禁店铺
    public Result<Void> banShop(Integer shopId) {
        try {
            boolean success = shopService.banShop(shopId);
            if (success) {
                return Result.success(null, "Shop has been banned successfully");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "Failed to ban the shop");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//解封店铺
    public Result<Void> unbanShop(Integer shopId) {
        try {
            boolean success = shopService.toggleShopStatus(shopId, "open");
            if (success) {
                return Result.success(null, "店铺已解封");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "解封店铺失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//封禁用户
    public Result<Void> banUser(Integer userId) {
        try {
            boolean success = userService.toggleUserStatus(userId, "banned");
            if (success) {
                return Result.success(null, "用户已封禁");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "封禁用户失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//解封用户
    public Result<Void> unbanUser(Integer userId) {
        try {
            boolean success = userService.toggleUserStatus(userId, "active");
            if (success) {
                return Result.success(null, "用户已解封");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "解封用户失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

// 审核租户充值信息
    public Result<Void> reviewRechargeApplications(Integer userId, boolean approve) {
        try {
            boolean success;
            if (approve) {
                success = userService.approveRecharge(userId);
            } else {
                success = userService.rejectRecharge(userId);
            }

            if (success) {
                return Result.success(null, approve ? "充值审核通过" : "充值审核拒绝");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "充值审核失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 获取签到统计信息
     * @return 签到统计信息
     */
    public Result<Map<String, Object>> getSignInStatistics() {
        try {
            Map<String, Object> statistics = signInService.getAllUsersSignInStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
   
    // 获取待审核的充值用户
    public Result<List<User>> getPendingRechargeUsers() {
        try {
            List<User> pendingUsers = userService.getPendingRechargeUsers();
            return Result.success(pendingUsers);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 获取用户签到记录
     * @param userId 用户ID
     * @return 签到记录列表
     */
    public Result<List<SignInRecord>> getUserSignInRecords(int userId) {
        try {
            List<SignInRecord> records = signInService.getSignInRecords(userId);
            return Result.success(records);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 获取签到趋势数据
     * @param days 天数
     * @return 签到趋势数据
     */
    public Result<List<Map<String, Object>>> getSignInTrend(int days) {
        try {
            List<Map<String, Object>> trend = signInService.getSignInTrend(days);
            return Result.success(trend);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 获取签到排行榜
     * @param limit 限制数量
     * @return 签到排行榜数据
     */
    public Result<List<Map<String, Object>>> getTopSignInUsers(int limit) {
        try {
            List<Map<String, Object>> topUsers = signInService.getTopSignInUsers(limit);
            return Result.success(topUsers);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 禁止用户签到
     * @param userId 用户ID
     * @return 操作结果
     */
    public Result<Void> banUserSignIn(int userId) {
        try {
            boolean success = signInService.banUserSignIn(userId);
            if (success) {
                return Result.success(null);
            } else {
                return Result.failure(ResultCode.OPERATION_FAILED, "禁止用户签到失败");
            }
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 解除用户签到限制
     * @param userId 用户ID
     * @return 操作结果
     */
    public Result<Void> unbanUserSignIn(int userId) {
        try {
            boolean success = signInService.unbanUserSignIn(userId);
            if (success) {
                return Result.success(null);
            } else {
                return Result.failure(ResultCode.OPERATION_FAILED, "解除用户签到限制失败");
            }
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
}